import express from "express";
import db from "../db/db.js";

const getFileRecords = express.Router();

function query() {
    return new Promise((resolve, reject) => {
        db.query("SELECT * FROM uploadFiles", (err, results) => {
            if (err) {
                return reject(err); // Reject the promise if there's an error
            }
            resolve(results); // Resolve the promise with results
        });
    });
}

getFileRecords.get("/fileRecords", async (req, res) => {
    try {
        const dataRes:any = await query(); // Wait for the query to complete
        res.send({
            code: 200,
            message: "get 请求成功！",
            result: dataRes,
            total: dataRes.length,
        });
    } catch (error) {
        console.error('Error fetching file records:', error);
        res.status(500).send({
            code: 1,
            message: "get 请求失败！",
            result: null,
        });
    }
});

export default getFileRecords;